package com.ohuang.demo.viewModel

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ohuang.demo.http.testApi
import com.ohuang.demo.http.toLogHttpCall
import com.ohuang.demo.http.toMainHttpCall
import com.ohuang.kthttp.call.asFlow
import com.ohuang.kthttp.call.getResult
import com.ohuang.kthttp.call.getResultOrNull
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.shareIn

class TestViewModel : ViewModel() {
    private val TAG = "TestViewModel"

    private var apiFlow=flow {
        emit(testApi.test().toLogHttpCall(request = {
            Log.d(TAG, "testApiFlow: request")
        }).getResultOrNull())
    }
    val testApiFlow =apiFlow.shareIn(viewModelScope, SharingStarted.WhileSubscribed(0,0), replay = 0)


    val testFlow = flow {
        var count = 0
        while (true) {
            delay(2000)
            emit("time=${count++}")


        }
    }
        .shareIn(viewModelScope, started = SharingStarted.WhileSubscribed(0,10000), replay = 10)
}